<script>
import { reactive } from "@vue/reactivity";
import { Toast } from "vant";
import { useStore } from "../store/index";
export default {
  props: ["order"],
  setup(props) {
    let data = reactive({
      orderItem: props.order,
    });

    // # 用户数据
    let userId = localStorage.getItem("P4_userId");
    let userData = useStore().userData[userId]; // # 当前用户

    // # 改变订单状态
    function change(msg, state) {
      Toast.loading({
        message: "请稍等...",
        forbidClick: true,
        loadingType: "spinner",
        onClose: () => {
          Toast.success(msg);
          data.orderItem.order_state = state;
        },
      });
    }

    // # 催发货
    let deliver = () => {
      change("已发货", "待收货");
    };

    // # 确认收货
    let receipt = () => {
      change("已收货", "已完成");
    };

    // # 取消订单
    let cancel = () => {
      change("已取消", "已取消");
    };

    let pay = () => {
      if (data.orderItem.pay_type == "钱包") {
        if (userData.balance > data.orderItem.allPrice) {
          userData.balance -= data.orderItem.allPrice;
          change("支付成功", "待发货");
        } else {
          Toast({
            message: "余额不足",
            position: "bottom",
          });
        }
      }
    };

    return {
      data,

      deliver,
      receipt,
      cancel,
      pay,
    };
  },
};
</script>


<template>
  <div class="order_item">
    <div class="order_id">
      订单号：{{ data.orderItem.orderId }}
      <span>{{ data.orderItem.order_state }}</span>
    </div>

    <router-link
      :to="'/order?id=' + data.orderItem.orderId"
      class="order_content"
    >
      <div class="shop_box">
        <div class="content">
          <div
            class="item"
            v-for="(item, i) in data.orderItem.shopList"
            :key="i"
          >
            <div class="img">
              <img :src="item.imgUrl" alt="" />
            </div>
            <div class="text">{{ item.size }}</div>
          </div>
        </div>
      </div>

      <div class="text">
        <div class="price">
          ￥<span>{{ data.orderItem.allPrice }}</span>
        </div>
        <div class="allNum">共{{ data.orderItem.allNum }}件商品</div>
      </div>
    </router-link>

    <div class="bottom_btn">
      <div
        class="btn cancel"
        @click="cancel"
        v-if="data.orderItem.order_state == '待付款'"
      >
        取消订单
      </div>
      <div
        class="btn pay"
        @click="pay"
        v-if="data.orderItem.order_state == '待付款'"
      >
        支付
      </div>
      <div
        class="btn deliver"
        @click="deliver"
        v-if="data.orderItem.order_state == '待发货'"
      >
        催发货
      </div>
      <div
        class="btn receipt"
        @click="receipt"
        v-if="data.orderItem.order_state == '待收货'"
      >
        确认收货
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.order_item {
  // $ 订单详情
  padding: 20px 15px 12px;
  border-radius: 15px;
  background: #fafafa;
  margin-bottom: 15px;

  // $ 顶部订单号
  > .order_id {
    font-size: 14px;
    line-height: 14px;
    font-weight: bold;
    display: flex;
    align-items: center;

    > span {
      font-size: 10px;
      color: #fff;

      font-weight: normal;
      padding: 5px 8px;
      margin-left: 3px;

      background-color: teal;
      border-radius: 10px;

      scale: 0.8;
    }
  }

  // $ 中间部分
  > .order_content {
    display: flex;
    padding-bottom: 10px;
    border-bottom: 1px dashed #cecece;

    // $ 商品列表
    > .shop_box {
      flex: 1;
      overflow-x: scroll;

      border-right: 1px solid #f0f0f0;

      &::-webkit-scrollbar {
        display: none;
      }

      > .content {
        display: flex;
        width: max-content;

        > .item {
          width: 80px;
          text-align: center;

          > .img {
            height: 80px;
            margin-right: 5px;

            > img {
              width: 100%;
              border-radius: 6px;
            }
          }

          > .text {
            width: 100%;
            font-size: 10px;
            font-weight: bold;
          }
        }
      }
    }

    // $ 总价格
    > .text {
      padding-left: 15px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;

      > .price {
        font-size: 16px;
        font-weight: bold;
      }

      > .allNum {
        font-size: 10px;
        color: #666;
        scale: 0.9;
        font-weight: bold;
      }
    }
  }

  // $ 底部按钮
  > .bottom_btn {
    display: flex;
    justify-content: flex-end;
    margin-top: 10px;

    > .btn {
      height: 34px;
      padding: 0 15px;
      margin-left: 5px;

      line-height: 34px;
      text-align: center;
      color: #fff;
      font-weight: bold;
      font-size: 12px;

      background: #56ddb0;
      border-radius: 12px;

      &:active {
        transform: translateY(-1px);
      }
    }

    > .cancel {
      color: #f9f9f9;
      background: #666;
    }

    > .pay {
      background: tomato;
      color: #fff;
    }

    > .deliver {
      // background: tan;
      border: 1px dashed #666;
    }

    > .receipt {
      background: paleturquoise;
    }
  }

  // $ 确认收货弹出层
  > .isGoPay {
    position: relative;

    > .content {
      width: 230px;
      height: 120px;
      overflow: hidden;

      > .question {
        height: 80px;
        line-height: 80px;
        font-size: 14px;
        font-weight: bold;
        text-align: center;
        border-bottom: 1px solid #e8e8e8;
      }

      > .bottom {
        display: flex;
        height: 40px;
        line-height: 40px;

        > .item {
          flex: 1;
          text-align: center;
          border-right: 1px solid #e8e8e8;
        }
      }
    }
  }
}
</style>